/*
Copyright (C) 2022-2024 Inspur Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/
package controller

import (
	"icep/auth"
	"icep/bean/param"
	"icep/common/constant"
	"icep/common/http"
	"strconv"

	"github.com/emicklei/go-restful"
)

func (ic *IcepController) LoginUser(req *restful.Request, resp *restful.Response) {
	userId := auth.GetCurrentUserId(req)
	tenantId := auth.GetCurrentTenantId(req)
	userType := auth.GetCurrentUserType(req)
	phone := auth.GetCurrentPhone(req)
	mail := auth.GetCurrentMail(req)
	userList := map[string]string{
		"userId":   userId,
		"tenantId": tenantId,
		"userType": userType,
		"phone":    phone,
		"mail":     mail,
	}
	http.OkWithData(resp, "获取成功", userList)
}

func (ic *IcepController) QueryUserList(req *restful.Request, resp *restful.Response) {
	currentUserType := auth.GetCurrentUserType(req)
	if currentUserType != constant.UserTypeAdmin {
		http.Failed(resp, "用户无权限！")
	}

	strPageSize := req.QueryParameter("pageSize")
	strPageNo := req.QueryParameter("pageNo")
	userType := req.QueryParameter("userType")
	phone := req.QueryParameter("phone")
	mail := req.QueryParameter("mail")
	status := req.QueryParameter("status")
	pageSize := 10
	if strPageSize != "" {
		pageSize, _ = strconv.Atoi(strPageSize)
	}
	pageNo := 1
	if strPageNo != "" {
		pageNo, _ = strconv.Atoi(strPageNo)
	}

	page := ic.IcepService.QueryUserList(userType, phone, mail, status, pageSize, pageNo)
	http.OkWithData(resp, "查询成功", page)
}

func (ic *IcepController) CreateUser(req *restful.Request, resp *restful.Response) {
	currentUserType := auth.GetCurrentUserType(req)

	currentUserTenantId := auth.GetCurrentTenantId(req)

	if currentUserType != constant.UserTypeAdmin {
		http.Failed(resp, "用户无权限！")
	}

	var userParam param.CreateUserParam

	userParam.TenantId = currentUserTenantId

	if err := req.ReadEntity(&userParam); err != nil {
		log.Errorf("create user read entity error: %v", err)
		http.Failed(resp, "请求参数不合法！")
		return
	}

	if userParam.UserType != constant.UserTypePerson && userParam.UserType != constant.UserTypeOrg {
		log.Errorf("only person user or org user can be created!")
		http.Failed(resp, "请求参数不合法！")
		return
	}

	if userParam.Password == "" {
		log.Errorf("usertype or password empty!")
		http.Failed(resp, "请求参数不合法！")
		return
	}

	if userParam.Phone == "" && userParam.Mail == "" {
		log.Errorf("phone and mail all empty!")
		http.Failed(resp, "请求参数不合法！")
		return
	}

	err := ic.IcepService.CreateUser(userParam)
	if err != nil {
		log.Errorf("create user error: %s", err)
		http.Failed(resp, err.Error())
		return
	}

	http.Ok(resp, "创建成功")
}

func (ic *IcepController) PutUser(req *restful.Request, resp *restful.Response) {

	currentUserType := auth.GetCurrentUserType(req)
	if currentUserType != constant.UserTypeAdmin {
		http.Failed(resp, "用户无权限！")
		return
	}
	var userParam param.PutUserParam
	if err := req.ReadEntity(&userParam); err != nil {
		log.Errorf("update user read entity error: %v", err)
		http.Failed(resp, "请求参数不合法！")
		return
	}
	err := ic.IcepService.PutUser(userParam)
	if err != nil {
		log.Errorf("update user error: %s", err)
		http.Failed(resp, "请求参数不合法！")
		return
	}

	http.Ok(resp, "修改成功")
}
